﻿using System;
using System.Linq;
using Complaint.Models.Dtos;

namespace Complaint.Repositories
{
    public class ContactRepository : BaseRepository<Contact>, IRepository<Contact>
    {
        #region IRepository<Contact> Members

        public void Create(Contact dto)
        {
            ObjectContext.AddToContact(dto);
            Update(dto);
        }

        public IQueryable<Contact> FindAll()
        {
            return ObjectContext.Contact;
        }

        public Contact FindById(int id)
        {
            return FindAll().FirstOrDefault(c => c.ContactID == id);
        }

        public IQueryable<Contact> FindByName(string name)
        {
            return from contact in FindAll()
                   where contact.Email.Contains(name) ||
                         contact.FirstName.Contains(name) ||
                         contact.LastName.Contains(name)
                   select contact;
        }

        #endregion

        public IQueryable<Contact> FindByOrganisation(int id)
        {
            return from contact in FindAll()
                   where contact.Organisation.OrganisationID == id
                   select contact;
        }

        public Contact FindByEmail(string email)
        {
            return FindAll().FirstOrDefault(c => c.Email == email);
        }
    }
}